home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / progbar.exe / TPROGBAR.CPP < prev    next >
C/C++ Source or Header  |  1992-10-12  |  5KB  |  179 lines

  1. /*=========================================================================
  2.    This TProgressBar class expands on Mr. Perez's upload TPROGB.ZIP. This
  3. alternative lets you include the class definition without having to 
  4. modify it for each main process. The dialog box that contructs TProgressBar
  5. will be responsible for updating it. See the example in EXAMPLE.CPP. You'll
  6. also find instructions on how to include the source in your TV.LIB and simply
  7. #define Uses_TProgressBar before #include <tv.h>.
  8.  
  9. By: Barnaby W. Falls
  10. CIS: 70662,1523
  11. -------------------------------------------------------------------------*/
  12.  
  13. #define Uses_TProgressBar
  14. #define Uses_TView
  15. #define Uses_TRect
  16. #define Uses_TGroup
  17. #define Uses_TDrawBuffer
  18. #define Uses_TStreamableClass
  19. #define Uses_opstream
  20. #define Uses_ipstream
  21. #include <tv.h>
  22.  
  23. #include <mem.h>         // memset
  24. #include <stdlib.h>      // itoa()
  25. #include <tprogbar.h>
  26.  
  27. __link( RView )
  28.  
  29. /* Registration object */
  30. TStreamableClass RProgressBar( TProgressBar::name,
  31.                               TProgressBar::build,
  32.                               __DELTA(TProgressBar)
  33.                             );
  34.  
  35. #define cpProgressBar "\x04"
  36. /*                       │
  37.                          └── Progress Bar Attrib Pair
  38.      cpProgressBar maps to TProgram::appPalette's index 4 which is, by
  39. default, used for the ScrollBar Page. cpProgressBar represents the normal
  40. progress bar color. In the constructor the foreground and background
  41. attributes are swapped to form the highlight color. Thus the highlight will
  42. always be the inverse of the bar color.
  43. */
  44.  
  45. const char * const near TProgressBar::name = "TProgressBar";
  46.  
  47. TProgressBar::TProgressBar(TRect& bounds, unsigned long aTotal, char abackChar) :
  48.    TView(bounds)
  49. {
  50.          backChar = abackChar;
  51.          total = aTotal;
  52.          numOffset = (size.x/2)-3;
  53.          bar = new char[size.x+1];
  54.          memset(bar,backChar,size.x);
  55.          bar[size.x] = '\0';
  56.          charValue = (double)100/(double)size.x;
  57.          progress =
  58.          curPercent =
  59.          curWidth = 0;
  60. }
  61.  
  62. TProgressBar::~TProgressBar(){
  63.    delete bar;
  64. }
  65.  
  66. void TProgressBar::draw() {
  67.    char string[4];
  68.    itoa(curPercent,string,10);
  69.    string[3] = '\0';
  70.    if(curPercent<10) {
  71.       string[2] = string[0];
  72.       string[1] = string[0] = ' ';
  73.       }
  74.    else if(curPercent<100 && curPercent>9) {
  75.       string[2] = string[1];
  76.       string[1] = string[0];
  77.       string[0] = ' ';
  78.       }
  79.    TDrawBuffer nbuf;
  80.    uchar colorNormal, colorHiLite;
  81.    colorNormal = getColor(1);
  82.    uchar fore = colorNormal >>4;                    // >>4 is same as /16
  83.    colorHiLite = fore+((colorNormal-(fore<<4))<<4); // <<4 is same as *16
  84.    nbuf.moveChar(0,backChar,colorNormal,size.x);
  85.    nbuf.moveStr(numOffset,string,colorNormal);
  86.    nbuf.moveStr(numOffset+3," %",colorNormal);
  87.    for(int i=0;i<curWidth;i++)
  88.       nbuf.putAttribute(i,colorHiLite);
  89.    writeLine(0, 0, size.x, 1, nbuf);
  90. }
  91.  
  92.  
  93. TPalette& TProgressBar::getPalette() const
  94. {
  95.    static TPalette palette( cpProgressBar, sizeof( cpProgressBar )-1 );
  96.    return palette;
  97. }
  98.  
  99.  
  100. void TProgressBar::update(unsigned long aProgress) {
  101.    progress = aProgress;
  102.    calcPercent();
  103.    drawView();
  104. }
  105.  
  106. void TProgressBar::calcPercent() {
  107.    unsigned int percent;
  108.    unsigned int width;
  109.  
  110.    // calculate the new percentage
  111.    percent = (int) ( ((double)progress/(double)total) * (double)100 );
  112.  
  113.    // percentage change?
  114.    if(percent!=curPercent) {
  115.       curPercent = percent;          // save new percentage
  116.       width = (int)((double)curPercent/charValue);// calculate percentage bar width
  117.  
  118.       // width change?
  119.       if(width!=curWidth) {
  120.      curWidth = width;          // save new width
  121.         }
  122.     }
  123. }
  124.  
  125. // return the maximum iteration
  126. unsigned long TProgressBar::getTotal() {
  127.    return total;
  128. }
  129.  
  130. // return the current iteration
  131. unsigned long TProgressBar::getProgress() {
  132.    return progress;
  133. }
  134.  
  135. // set a new maximum iteration & update display
  136. void TProgressBar::setTotal(unsigned long newTotal)
  137. {
  138.    unsigned long tmp = total;
  139.    total = newTotal;
  140.    memset(bar,backChar,size.x);
  141.    curWidth   = 0;                    // current width of percentage bar
  142.    progress   = 0;                    // current iteration
  143.    curPercent = 0;                    // current percentage
  144.    if(tmp)                // since it starts with 0, only update if changing
  145.       drawView();                       // update the thermometer bar display
  146. }
  147.  
  148. // set a new current iteration & update display
  149. void TProgressBar::setProgress(unsigned long newProgress) {
  150.    unsigned int percent;
  151.    unsigned int width;
  152.    progress = newProgress;
  153.    calcPercent();
  154.    drawView();                       // paint the thermometer bar
  155. }
  156.  
  157. void TProgressBar::write( opstream& os )
  158. {
  159.     TView::write( os );
  160.     os.writeString( bar );
  161.     os << backChar << total << progress << dispLen <<
  162.           curPercent << curWidth << numOffset << charValue;
  163. }
  164.  
  165. void *TProgressBar::read( ipstream& is )
  166. {
  167.     TView::read( is );
  168.     bar = is.readString();
  169.     is >> backChar >> total >> progress >> dispLen >>
  170.           curPercent >> curWidth >> numOffset >> charValue;
  171.     return this;
  172. }
  173.  
  174. TStreamable *TProgressBar::build()
  175. {
  176.     return new TProgressBar( streamableInit );
  177. }
  178.  
  179.